<?php
/**
 * 卖家活动管理
 *
 *
 *
 * * @网店运维 (c) 2015-2018  Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */



defined('InShopNC') or exit('Access Invalid!');
class store_activityControl extends BaseSellerControl {
    public function __construct() {
        parent::__construct();
        Language::read('member_store_index');
    }

    public function indexOp() {
        $this->store_activityOp();
    }

    /**
     * 活动管理
     */
    public function store_activityOp(){
        $page   = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');
        $activity   = Model('activity');
        //活动为商品活动，并且为开启状态
        $list   = $activity->getList(array('activity_type'=>'1','opening'=>false,'order'=>'activity.activity_sort asc','store_id'=>$_SESSION['store_id']),$page);
        /**
         * 页面输出
         */
        Tpl::output('list',$list);
        Tpl::output('show_page',$page->show());
        self::profile_menu('store_activity');
        Tpl::showpage('store_activity.list');
    }
    /**
     * 参与活动
     */
    public function activity_applyOp(){
        //根据活动编号查询活动信息
        $activity_id = intval($_GET['activity_id']);
        if($activity_id <= 0){
            showMessage(Language::get('para_error'),'index.php?act=store_activity&op=store_activity','html','error');
        }
        $activity_model = Model('activity');
        $activity_info  = $activity_model->getOneById($activity_id);
        //活动类型必须是商品并且活动没有关闭并且活动进行中
        if(empty($activity_info) || $activity_info['activity_type'] != '1' || !in_array($activity_info['activity_state'] ,array(1,2)) || $activity_info['activity_end_date']<time()){ // || $activity_info['activity_start_date']>time()
            showMessage(Language::get('store_activity_not_exists'),'index.php?act=store_activity&op=store_activity','html','error');
        }
        Tpl::output('activity_info',$activity_info);
        $list   = array();//声明存放活动细节的数组
        //查询商品分类列表
        $gc = Model('goods_class');
        $gc_list    = $gc->getTreeClassList(3);
        foreach($gc_list as $k=>$gc){
            $gc_list[$k]['gc_name'] = '';
            $gc_list[$k]['gc_name'] = str_repeat("&nbsp;",$gc['deep']*2).$gc['gc_name'];
        }
        Tpl::output('gc_list',$gc_list);
        //查询品牌列表
        $brand  = Model('brand');
        $brand_list = $brand->getBrandList(array());
        Tpl::output('brand_list',$brand_list);
        //查询活动细节信息
        $activity_detail_model  = Model('activity_detail');
        $list   = $activity_detail_model->getGoodsJoinList(array('activity_id'=>"$activity_id",'store_id'=>"{$_SESSION['store_id']}",'activity_detail_state_in'=>"'0','1','3'",'group'=>'activity_detail_state asc'));
        //构造通过与审核中商品的编号数组,以便在下方待选列表中,不显示这些内容
        $item_ids   = array();
        if(is_array($list) and !empty($list)){
            foreach($list as $k=>$v){
                $item_ids[] = $v['item_id'];
            }
        }
        Tpl::output('list',$list);

        //根据查询条件查询商品列表
        $condition  = array();
        if($_GET['gc_id']!=''){
            $condition['gc_id'] = intval($_GET['gc_id']);
        }
        if($_GET['brand_id']!=''){
            $condition['brand_id']  = intval($_GET['brand_id']);
        }
        if(trim($_GET['name'])!=''){
            $condition['goods_name'] = array('like' ,'%'.trim($_GET['name']).'%');
        }
        $condition['store_id']      = $_SESSION['store_id'];
        if (!empty($item_ids)){
            $condition['goods_id']  = array('not in', $item_ids);
        }
        $model_goods    = Model('goods');
        $goods_list = $model_goods->getGoodsOnlineList($condition,'*', 10);
        Tpl::output('goods_list',$goods_list);
        Tpl::output('show_page',$model_goods->showpage());
        Tpl::output('search',$_GET);
        /**
         * 页面输出
         */
        self::profile_menu('activity_apply');
        Tpl::showpage('store_activity.apply');
    }

    /**
     * 活动申请保存
     */
    public function activity_apply_saveOp(){
        //判断页面参数
        if(empty($_POST['item_id'])){
            showDialog(Language::get('store_activity_choose_goods'),'index.php?act=store_activity&op=store_activity');
        }
        $activity_id = intval($_POST['activity_id']);
        if($activity_id <= 0){
            showDialog(Language::get('para_error'),'index.php?act=store_activity&op=store_activity');
        }
        //根据页面参数查询活动内容信息，如果不存在则添加，存在则根据状态进行修改
        $activity_model = Model('activity');
        $activity   = $activity_model->getOneByid($activity_id);
        //活动类型必须是商品并且活动没有关闭并且活动进行中
        if(empty($activity) || $activity['activity_type'] != '1' || !in_array($activity['activity_state'],array(1,2)) || $activity['activity_end_date']<time()){//$activity['activity_start_date']>time() ||
            showDialog(Language::get('store_activity_not_exists'),'index.php?act=store_activity&op=store_activity');
        }
        $activity_detail    = Model('activity_detail');
        $list   = $activity_detail->getList(array('store_id'=>"{$_SESSION['store_id']}",'activity_id'=>"$activity_id"));
        $ids    = array();//已经存在的活动内容编号
        $ids_state2 = array();//已经存在的被拒绝的活动编号
        if(is_array($list) and !empty($list)){
            foreach ($list as $ad){
                $ids[]  = $ad['item_id'];
                if($ad['activity_detail_state']=='2'){
                    $ids_state2[]   = $ad['item_id'];
                }
            }
        }
        //根据查询条件查询商品列表
        foreach ($_POST['item_id'] as $item_id){
            $item_id = intval($item_id);
            if(!in_array($item_id,$ids)){
                $input  = array();
                $input['activity_id']   = $activity_id;
                $goods  = Model('goods');
                $item   = $goods->getGoodsOnlineInfoByID($item_id, 'goods_name,store_id,store_name');
                if(empty($item) || $item['store_id'] != $_SESSION['store_id']){
                    continue;
                }
                $input['item_name'] = $item['goods_name'];
                $input['item_id']   = $item_id;
                $input['store_id']  = $item['store_id'];
                $input['store_name']= $item['store_name'];
                $activity_detail->add($input);
            }elseif(in_array($item_id,$ids_state2)){
                $input  = array();
                $input['activity_detail_state']= '0';//将重新审核状态去除
                $activity_detail->updateList($input,array('item_id'=>$item_id));
            }
        }
        showDialog(Language::get('store_activity_submitted'),'reload','succ');
    }

    public function activity_addOp(){
        //新建处理
        if($_POST['form_submit'] != 'ok'){
            Tpl::showpage('activity.add','null_layout');
            exit;
        }

        //提交表单
        $obj_validate = new Validate();
        $validate_arr[] = array("input"=>$_POST["activity_title"],"require"=>"true","message"=>Language::get('activity_new_title_null'));
        $validate_arr[] = array("input"=>$_POST["activity_start_date"],"require"=>"true","message"=>Language::get('activity_new_startdate_null'));
        $validate_arr[] = array("input"=>$_POST["activity_end_date"],"require"=>"true",'validator'=>'Compare','operator'=>'>','to'=>"{$_POST['activity_start_date']}","message"=>Language::get('activity_new_enddate_null'));
        $validate_arr[] = array('input'=>$_FILES['activity_banner']['name'],'require'=>'true','message'=>Language::get('activity_new_banner_null'));

        $obj_validate->validateparam = $validate_arr;
        $error = $obj_validate->validate();
        if ($error != ''){
            showMessage(Language::get('error').$error,'','','error');
        }
        $upload = new UploadFile();
        $upload->set('default_dir',ATTACH_ACTIVITY);
        $result = $upload->upfile('activity_banner');
        if(!$result){
            showMessage($upload->error);
        }
        //保存
        $input  = array();
        $input['activity_title']    = trim($_POST['activity_title']);
        //$input['activity_type']       = trim($_POST['activity_type']);
        $input['activity_type']     = '1';
        $input['activity_banner']   = $upload->file_name;
        $input['activity_style']    = 'default_style';//trim($_POST['activity_style']);
        $input['activity_desc']     = '';//trim($_POST['activity_desc']);
        $input['activity_sort']     = 1;//intval(trim($_POST['activity_sort']));
        $input['activity_start_date']= strtotime(trim($_POST['activity_start_date']));
        $input['activity_end_date'] = strtotime(trim($_POST['activity_end_date']));
        $input['activity_state']    = 2;//intval($_POST['activity_state']);
        $input['member_id'] = $_SESSION['member_id'];
        $input['member_name'] = $_SESSION['member_name'];
        $input['store_id'] = $_SESSION['store_id'];
        $input['store_name'] = $_SESSION['store_name'];

        $activity   = Model('activity');
        $result = $activity->add($input);
        if($result){
            $this->recordSellerLog('新增预售活动，活动名称：'. $input['activity_title'].'，活动编号：'.$result);
            showMessage(Language::get('nc_common_op_succ'),'index.php?act=store_activity&op=store_activity');
        }else{
            //添加失败则删除刚刚上传的图片,节省空间资源
            @unlink(BASE_UPLOAD_PATH.DS.ATTACH_ACTIVITY.DS.$upload->file_name);
            showMessage(Language::get('nc_common_op_fail'));
        }
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string    $menu_type  导航类型
     * @param string    $menu_key   当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_key='') {
        Language::read('member_layout');
        $menu_array = array(
                1=>array('menu_key'=>'store_activity','menu_name'=>Language::get('nc_member_path_activity_list'),'menu_url'=>'index.php?act=store_activity&op=store_activity'),
                2=>array('menu_key'=>'activity_apply','menu_name'=>Language::get('nc_member_path_join_activity'),'menu_url'=>'')
        );
        if($menu_key == 'store_activity') {
            unset($menu_array[2]);
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }
}
